//==============================================================================
#include "NotificationList.h"

using namespace std;
using namespace ui;
//==============================================================================

NotificationList::NotificationList( int x, int y, int w, int h): Component(x, y, w, h, NONE, "") {
	this->notifications= new LISTNOT();
}
//------------------------------------------------------------------------------
void NotificationList::addNotification(string m){
	Notification newNotification;
	newNotification.posX = this->getXCoordinate();
	newNotification.posY = this->getYCoordinate();
	newNotification.counter = 250;
	newNotification.message = m;	
	if(this->notifications->empty()){
		this->notifications->push_back(newNotification);
	}else{
		
		LISTNOT::iterator it;
		it= this->notifications->begin();
		while (it != this->notifications->end()){
			it->posY += 12;	
			it++;
		}
		this->notifications->push_front(newNotification);
	}
}
//------------------------------------------------------------------------------
void NotificationList::draw(SDL_Surface* s) throw (SDL_Exception)
{
    try
    {
	int fontSize = FONT_SIZE;
	LISTNOT::iterator it;
	it= this->notifications->begin();
	while (it != this->notifications->end()){
		TTF_Font* font = TTF_OpenFont("Arial.ttf", fontSize);
	    if(!font){
		    std::string errorMessage("TEXTBOX - TTF_OpenFont - ERROR");
		    throw(SDL_Exception(errorMessage));
		}
		SDL_Color textColor = {129, 255, 0};
		SDL_Surface* message = TTF_RenderText_Solid(font, it->message.c_str(), textColor);
		TTF_CloseFont(font);
		SDL_Rect textArea;
		textArea.x = it->posX;
		textArea.y = it->posY;
		if(SDL_BlitSurface(message, NULL, s, &textArea)){
			SDL_FreeSurface(message);
			std::string errorMessage("TEXTBOX - BLIT SURFACE MESSAGE - ERROR");
			throw SDL_Exception (errorMessage);
		}
		it->counter--;
		it++;
	}
    }
    catch( SDL_Exception &ex)
    {
        throw ex;
    }
	LISTNOT::iterator it;
	it= this->notifications->begin();
	while (it != this->notifications->end()){
		LISTNOT::iterator auxIt =it;
		it++;
		if (auxIt->counter <= 0) {
			this->notifications->erase(auxIt);
		}
	}
}
//------------------------------------------------------------------------------
NotificationList::~NotificationList(void) {
}

COMPONENT_TYPE NotificationList::handleEvent(SDL_Event* s){
	
	return this->type;
}
//==============================================================================